import requests
from starlette.requests import Request
from typing import Dict
from fastapi import FastAPI
from sklearn.datasets import load_iris
from sklearn.ensemble import GradientBoostingClassifier

from ray import serve

# Train model.
iris_dataset = load_iris()
model = GradientBoostingClassifier()
model.fit(iris_dataset["data"], iris_dataset["target"])
app = FastAPI()

@serve.deployment(route_prefix="/")
@serve.ingress(app)
class BoostingModel:
    def __init__(self):
        self.model = model
        self.label_list = iris_dataset["target_names"].tolist()

    @app.get("/iris")
    def iris(self, vector):
        print(vector)
        # print(f"Received http request with data {payload}")
        # print(self.label_list)
        #
        # prediction = self.model.predict([payload])[0]
        # print(prediction)
        # human_name = self.label_list[prediction]
        return {"result": vector}

    @app.get("/")
    def root(self):
        return "Hello, world!"


# Deploy model.
serve.run(BoostingModel.bind())
input("running...")
# Query it!
sample_request_input = {"vector": [1.2, 1.0, 1.1, 0.9]}
response = requests.get(
    "http://localhost:8000/iris", json=sample_request_input)
print(response.text)